package com.example.mac.yuejian14.model;

import java.util.List;

import com.example.mac.yuejian14.listener.RetrofitListener1;
import com.example.mac.yuejian14.bean.MatchBean;
import com.example.mac.yuejian14.service.MatchService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/25.
 */

public class MatchModel {
    private Retrofit retrofit;
    public MatchModel(){
        //创建实例
        retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2/neu12_yuejian/index.php/Api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //比赛列表
    public Call<List<MatchBean>> getMatchList(){
        MatchService matchService = retrofit.create(MatchService.class);
        return matchService.getMatchList();
    }

    //获取比赛详情
    public void getMatchById(String matid, final RetrofitListener1 listener){
        MatchService matchService = retrofit.create(MatchService.class);
        Call call = matchService.getMatchById(matid);
        Callback<MatchBean> callback = new Callback<MatchBean>() {
            @Override
            public void onResponse(Call<MatchBean> call, Response<MatchBean> response) {
                MatchBean getMatchById = response.body();
                listener.onSuccess(getMatchById,1);
            }
            @Override
            public void onFailure(Call<MatchBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }
    //搜索
    public Call getMatchBySearch(String matname){
        MatchService matchService= retrofit.create(MatchService.class);
        return matchService.getMatchBySearch(matname);
    }

    //报名
    public Call addMatch(int uid, int matid){
        MatchService matchService= retrofit.create(MatchService.class);
        return matchService.addMatch(uid,matid);
    }
}
